using Microsoft.AspNetCore.Mvc;
using MyHomeworkWebAPi.Models.ef;
using MyHomeworkWebAPi.Models.Request;
using MyHomeworkWebAPi.Models.response;
using MyHomeworkWebAPi.Services;

namespace MyHomeworkWebAPi.Controllers
{
    /// <summary>
    /// 订单管理控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    public class OrdersController : ControllerBase
    {
        private readonly IOrderServices _orderServices;

        /// <summary>
        /// 构造函数，注入订单服务
        /// </summary>
        /// <param name="orderServices">订单服务接口</param>
        public OrdersController(IOrderServices orderServices)
        {
            _orderServices = orderServices;
        }

        /// <summary>
        /// 获取所有订单
        /// </summary>
        /// <returns>订单列表</returns>
        [HttpGet]
        public async Task<IActionResult> GetAll()
        {
            var orders = await _orderServices.GetAllAsync();
            return Ok(ApiResponse<List<Order>>.SuccessResponse(orders));
        }

        /// <summary>
        /// 根据ID获取订单
        /// </summary>
        /// <param name="id">订单ID</param>
        /// <returns>订单详情</returns>
        [HttpGet("{id}")]
        public async Task<IActionResult> GetById(Guid id)
        {
            var order = await _orderServices.GetByIdAsync(id);
            if (order == null)
            {
                return NotFound(ApiResponse<Order>.ErrorResponse("订单不存在", 404));
            }
            return Ok(ApiResponse<Order>.SuccessResponse(order));
        }

        /// <summary>
        /// 创建订单
        /// </summary>
        /// <param name="request">订单创建请求</param>
        /// <returns>创建的订单信息</returns>
        [HttpPost]
        public async Task<IActionResult> Create([FromBody] OrderCreateRequest request)
        {
            try
            {
                var order = await _orderServices.CreateAsync(request);
                return Ok(ApiResponse<Order>.SuccessResponse(order, "订单创建成功"));
            }
            catch (Exception ex)
            {
                return BadRequest(ApiResponse<Order>.ErrorResponse(ex.Message, 400));
            }
        }

        /// <summary>
        /// 更新订单状态
        /// </summary>
        /// <param name="id">订单ID</param>
        /// <param name="status">新状态</param>
        /// <returns>更新后的订单信息</returns>
        [HttpPut("{id}/status")]
        public async Task<IActionResult> UpdateStatus(Guid id, [FromQuery] string status)
        {
            try
            {
                var order = await _orderServices.UpdateStatusAsync(id, status);
                return Ok(ApiResponse<Order>.SuccessResponse(order, "状态更新成功"));
            }
            catch (Exception ex)
            {
                return NotFound(ApiResponse<Order>.ErrorResponse(ex.Message, 404));
            }
        }

        /// <summary>
        /// 获取用户的订单列表
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <returns>该用户的订单列表</returns>
        [HttpGet("user/{userId}")]
        public async Task<IActionResult> GetByUserId(Guid userId)
        {
            var orders = await _orderServices.GetByUserIdAsync(userId);
            return Ok(ApiResponse<List<Order>>.SuccessResponse(orders));
        }

        /// <summary>
        /// 获取商品的订单列表
        /// </summary>
        /// <param name="productId">商品ID</param>
        /// <returns>该商品的订单列表</returns>
        [HttpGet("product/{productId}")]
        public async Task<IActionResult> GetByProductId(Guid productId)
        {
            var orders = await _orderServices.GetByProductIdAsync(productId);
            return Ok(ApiResponse<List<Order>>.SuccessResponse(orders));
        }
    }
} 